package ch9;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XMLCreate {

	public static void main(String[] args) {
		final String fileName = "c.xml";
		// SAXReader reader = new SAXReader();
		Document doc = DocumentHelper.createDocument();
		System.out.println(doc.getClass().getName());
		Element root = doc.addElement("clothes");
		Element first = root.addElement("cloth");
		first.addAttribute("chandi", "温州");
		first.addAttribute("color", "黄色");
		first.addElement("brand").setText("江南皮革厂");
		first.addElement("price").setText("20");
		first.addElement("suit").setText("老少皆宜");
		OutputFormat format = OutputFormat.createPrettyPrint();
		format.setEncoding("utf-8");
		XMLWriter xm;
		try {
			xm = new XMLWriter(new FileWriter(fileName), format);
			xm.write(doc);
			xm.close();
			System.out.println("over");
		} catch (IOException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}

		// try {
		// Document doc = reader.read(new File("c.xml"));
		// } catch (DocumentException e) {
		// // TODO 自动生成的 catch 块
		// e.printStackTrace();
		// }

	}

}
